package org.papervision3d.objects
{
    import org.papervision3d.core.*;
    import org.papervision3d.core.proto.*;
    import org.papervision3d.core.geom.*;
    
    import flash.display.BitmapData;
    
    // The Plane class lets you create and display flat rectangle objects.
    // The rectangle can be divided in smaller segments. This is usually done to reduce linear mapping artifacts.
    // Dividing the plane in the direction of the perspective or vanishing point, helps to reduce this problem. Perspective distortion dissapears when the plane is facing straignt to the camera, i.e. it is perpendicular with the vanishing point of the scene.
    public class Plane extends Mesh3D
    {
        // Number of segments horizontally. Defaults to 1.
        public var segmentsW:Number;
    
        // Number of segments vertically. Defaults to 1.
        public var segmentsH:Number;
    
        // Default size of Plane if not texture is defined.
        static public var DEFAULT_SIZE:Number = 500;
    
        // Default size of Plane if not texture is defined.
        static public var DEFAULT_SCALE:Number = 1;
    
        // Default value of gridX if not defined. The default value of gridY is gridX.
        static public var DEFAULT_SEGMENTS:Number = 1;
    	
    	
        // Create a new Plane object.
        // @param    material    A MaterialObject3D object that contains the material properties of the object.
        // @param    width       [optional] - Desired width or scaling factor if there's bitmap texture in material and no height is supplied.
        // @param    height      [optional] - Desired height.
        // @param    segmentsW   [optional] - Number of segments horizontally. Defaults to 1.
        // @param    segmentsH   [optional] - Number of segments vertically. Defaults to segmentsW.
        // @param    initObject  [optional] - An object that contains user defined properties with which to populate the newly created GeometryObject3D.
        // It includes x, y, z, rotationX, rotationY, rotationZ, scaleX, scaleY scaleZ and a user defined extra object.
        // If extra is not an object, it is ignored. All properties of the extra field are copied into the new instance. The properties specified with extra are publicly available.
        public function Plane(material:MaterialObject3D = null, width:Number = 0, height:Number = 0, segmentsW:Number = 0, segmentsH:Number = 0, initObject:Object = null)
        {
            super(material, null, initObject);
    
            this.segmentsW = segmentsW || DEFAULT_SEGMENTS; // Defaults to 1
            this.segmentsH = segmentsH || this.segmentsW;   // Defaults to segmentsW
    
            var scale:Number = DEFAULT_SCALE;
    
            if (!height)
            {
                if (width)
                    scale = width;
    
                if(material && material.bitmap)
                {
                    width  = material.bitmap.width  * scale;
                    height = material.bitmap.height * scale;
                }
                else
                {
                    width  = DEFAULT_SIZE * scale;
                    height = DEFAULT_SIZE * scale;
                }
            }
    
            buildPlane(width, height);
        }
    
        private function buildPlane(width:Number, height:Number):void
        {
            var gridX    :Number = this.segmentsW;
            var gridY    :Number = this.segmentsH;
            var gridX1   :Number = gridX + 1;
            var gridY1   :Number = gridY + 1;
    
            var vertices :Array  = this.vertices;
            var faces    :Array  = this.faces;
    
            var textureX :Number = width / 2;
            var textureY :Number = height / 2;
    
            var iW       :Number = width / gridX;
            var iH       :Number = height / gridY;
    
            // Vertices
            for (var ix:int = 0; ix < gridX + 1; ix++)
            {
                for (var iy:int = 0; iy < gridY1; iy++)
                {
                    var x:Number = ix * iW - textureX;
                    var y:Number = iy * iH - textureY;
    
                    vertices.push(new Vertex3D(x, y, 0));
                }
            }
    
            // Faces
            var uvAAA:NumberUV;
            var uvCCC:NumberUV;
            var uvBBB:NumberUV;
    
            for (ix = 0; ix < gridX; ix++)
            {
                for (iy= 0; iy < gridY; iy++)
                {
                    // Triangle A
                    var aaa:Vertex3D = vertices[ix     * gridY1 + iy    ]; 
                    var ccc:Vertex3D = vertices[ix     * gridY1 + (iy+1)]; 
                    var bbb:Vertex3D = vertices[(ix+1) * gridY1 + iy    ];
    
                    uvAAA = new NumberUV(ix     / gridX, iy     / gridY);
                    uvCCC = new NumberUV(ix     / gridX, (iy+1) / gridY);
                    uvBBB = new NumberUV((ix+1) / gridX, iy     / gridY);
    
                    faces.push(new Face3D([aaa, bbb, ccc], null, [uvAAA, uvBBB, uvCCC]));
    
                    // Triangle B
                    aaa = vertices[(ix+1) * gridY1 + (iy+1)];
                    ccc = vertices[(ix+1) * gridY1 + iy    ];
                    bbb = vertices[ix     * gridY1 + (iy+1)];
    
                    uvAAA =  new NumberUV((ix+1) / gridX, (iy+1) / gridY);
                    uvCCC =  new NumberUV((ix+1) / gridX, (iy)   / gridY);
                    uvBBB =  new NumberUV((ix)   / gridX, (iy+1) / gridY);
    
                    faces.push(new Face3D([aaa, bbb, ccc], null, [uvAAA, uvBBB, uvCCC]));
                }
            }
        }
    }
}
